package com.example.woke1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ZhcaihuoqiActivity extends Activity implements OnClickListener {

    private Intent intent;
    private MyApp application;
    private TextView mTyesterday, mTmoney, mTtotl;
    private String totalmoney, totalmoney1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhcaihuoqi);

        application = (MyApp) getApplication();
        intent = new Intent();
        intview();

    }

    private void intview() {
        findViewById(R.id.avzhcaihuoqi_image_back).setOnClickListener(this);
        findViewById(R.id.avzhcaihuoqi_text_in).setOnClickListener(this);
        findViewById(R.id.avzhcaihuoqi_text_out).setOnClickListener(this);
        mTyesterday = (TextView) findViewById(R.id.avzhcaihuoqi_text_yesterday);
        mTtotl = (TextView) findViewById(R.id.avzhcaihuoqi_text_total);
        mTmoney = (TextView) findViewById(R.id.avzhcaihuoqi_text_money);
        getpersonhuoqi();
    }

    private void getpersonhuoqi() {
        if (application.getDatas_load() == null) return;
        AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcaihuoqiActivity.this);
        ahc.setCookieStore(myCookieStore);
        RequestParams params = new RequestParams();
        params.put("g", "api");
        params.put("m", "financial");
        params.put("a", "huoqiDetail");
        params.put("userid", application.getDatas_load().getId());
        Log.e("params", params.toString());
        ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("定期", "成功" + response);
                try {
                    String flag = response.getString("flag");
                    String info = response.getString("info");
                    if (flag.equals("success")) {
                        //Type:1����2���� money���total_proceeds�ۼ�����yesterday��������
                        JSONObject jsonObject = response.getJSONObject("data");
                        JSONObject object = jsonObject.getJSONObject("data");
                        String id = object.getString("id");
                        String type = object.getString("type");
                        String money = object.getString("money");
                        String total_proceeds = object.getString("total_proceeds");
                        String yesterday = object.getString("yesterday");
                        String finan_id = object.getString("finan_id");
                        String userid = object.getString("userid");
                        String createdate = object.getString("createdate");
                        if (money.contains(".") && money.length() - money.indexOf(".") > 2) {
                            totalmoney = money.substring(0, money.length() - 3);
                            totalmoney1 = total_proceeds.substring(0, total_proceeds.length() - 3);
                            yesterday = yesterday.substring(0, yesterday.length() - 3);
                        } else {
                            totalmoney = money;
                            totalmoney1 = total_proceeds;
                        }
                        mTmoney.setText(totalmoney);
                        mTyesterday.setText(yesterday);
                        mTtotl.setText(totalmoney1);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(ZhcaihuoqiActivity.this, "������δ��Ӧ", Toast.LENGTH_SHORT).show();
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
            }

            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
//				mProgress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish() {
                super.onFinish();
//				mProgress.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//		if(requestCode == 10 && resultCode == 11){
        getpersonhuoqi();
//		}

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.avzhcaihuoqi_image_back:
                finish();
                break;

            case R.id.avzhcaihuoqi_text_in:
                intent.setClass(ZhcaihuoqiActivity.this, ZhcailicaiinActivity.class);
                startActivityForResult(intent, 10);
                break;

            case R.id.avzhcaihuoqi_text_out:
                if (!TextUtils.isEmpty(totalmoney) && totalmoney.matches("\\d*|\\d*.|^[1-9]\\d*.\\d*|0.\\d*[1-9]|0?.0+|0")) {
                    float parseFloat = Float.parseFloat(totalmoney);
                    if (parseFloat > 0) {
                        intent.setClass(ZhcaihuoqiActivity.this, ZhcailicaioutActivity.class);
                        intent.putExtra("totalmoney", parseFloat);
                        startActivityForResult(intent, 10);
                    } else {
                        Toast.makeText(ZhcaihuoqiActivity.this, "账户余额不足", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(this,"收益金额"+totalmoney+"过小,暂不支持转出",Toast.LENGTH_SHORT).show();
                }
                break;

            default:
                break;
        }
    }
}
